package fr.glre.tartopom;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TourneViteActivity extends Activity implements SensorEventListener {

	TextView tv1;
	TextView tvG;
	TextView tvD;
	Button bouton;

	private Handler handler321;
	private Handler handlerturn;
	private int compteur;
	private int nbTours;
	private int delay;

	private int goal;
	private boolean dir;
	private boolean pointOk;
	private double startX;
	private double x;

	private SensorManager sm;
	private PowerManager pm;
	PowerManager.WakeLock wl;

	private Runnable troisdeuxungo = new Runnable() {
		@Override
		public void run() {
			if (compteur != 0) {
				tv1.setText("" + compteur);
				compteur--;

				handler321.postDelayed(this, 1000);
			} else {
				tv1.setText("Vous devez tourner votre t�l�phone : ");

				// start();

				compteur = 3;
				handlerturn.postDelayed(turning, 1000);
			}
		}
	};

	private Runnable turning = new Runnable() {
		@Override
		public void run() {

			if (pointOk) {
				pointOk = false;
				goal = (int) (Math.random() * 90.);
				dir = Math.random() < 0.5;

				tvG.setText("" + goal + " degr�s\n GAUCHE");
				tvD.setText("" + goal + " degr�s\n DROITE");

				if (dir) {
					tvG.setVisibility(TextView.INVISIBLE);
					tvD.setVisibility(TextView.VISIBLE);
				} else {
					tvD.setVisibility(TextView.INVISIBLE);
					tvG.setVisibility(TextView.VISIBLE);
				}
				nbTours++;
				if (nbTours % 5 == 0) {
					delay -= 1000;
				}
				if (delay < 1000) {
					delay = 1000;
				}
				handlerturn.postDelayed(turning, delay);
			} else {
				tvG.setVisibility(TextView.INVISIBLE);
				tvD.setVisibility(TextView.INVISIBLE);
				tv1.setText("perdu !!!");
			}
		}
	};

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tournevite);

		sm = (SensorManager) getSystemService(SENSOR_SERVICE);
		sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
				SensorManager.SENSOR_DELAY_GAME);

		pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "resteaplat");
		wl.acquire();

		tv1 = (TextView) findViewById(R.id.tvB);
		tvG = (TextView) findViewById(R.id.tvGB);
		tvD = (TextView) findViewById(R.id.tvDB);
		bouton = (Button) findViewById(R.id.viteB);

		tv1.setText("Quand vous �tes pr�t, appuyez sur le bouton.");

		bouton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				bouton.setVisibility(Button.INVISIBLE);

				handler321 = new Handler();
				handlerturn = new Handler();
				nbTours = 0;
				delay = 10000;
				compteur = 3;
				startX = x;
				handler321.postDelayed(troisdeuxungo, 1000);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.tournevitemenu, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.item1:
			Intent intent = new Intent(this.getBaseContext(),
					TournePomActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.item2:
			Toast.makeText(
					this,
					"Prenez le téléphone et appuyez sur le"
							+ " bouton \"C\'est parti !\". L'application vous indiquera"
							+ " une direction et un angle, et vous devrez tourner le"
							+ "téléphone jusqu'à atteindre la bonne position, avant le"
							+ " temps limite.", Toast.LENGTH_LONG).show();
			break;
		case R.id.item3:
			System.exit(RESULT_OK);
			break;
		}

		return false;
	}

	@Override
	public void onPause() {
		super.onPause();
		wl.release();
		sm.unregisterListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		wl.acquire();
		sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
				SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		int sensor = event.sensor.getType();
		float[] values = event.values;

		synchronized (this) {
			if (sensor == Sensor.TYPE_ORIENTATION) {
				x = values[0];
			}
		}
		if (dir) {
			if (Math.round(x) == Math.round(startX) - goal) {
				pointOk = true;
			}
		} else {
			if (Math.round(x) == Math.round(startX) + goal) {
				pointOk = true;
			}
		}
	}
}
